package com.bjsxt.shop.head.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.shop.common.controller.BaseController;
import com.bjsxt.shop.common.util.ConstatFinalUtil;
import com.bjsxt.shop.goods.pojo.AGoods;
import com.bjsxt.shop.orders.pojo.AOrders;
import com.bjsxt.shop.orders.pojo.AOrdersGoods;
import com.bjsxt.shop.orders.service.IOrdersService;
import com.bjsxt.shop.users.pojo.AUsers;
import com.bjsxt.shop.users.pojo.AUsersAddress;
import com.bjsxt.shop.users.pojo.AUsersAddressEnum;
import com.bjsxt.shop.users.pojo.AUsersCar;
import com.bjsxt.shop.users.service.IUsersService;

/**
 * 管理员登陆后的Controller
 * 
 * @author wangshSxt
 *
 */
@Controller
@RequestMapping("/head/orders")
public class OrdersHeadController extends BaseController
{
	@Resource
	private IUsersService usersService;
	@Resource
	private IOrdersService ordersService;
	
	/**
	 * 订单确认
	 * 
	 * @return
	 */
	@RequestMapping("/ordersConfirm")
	public String ordersConfirm(HttpServletRequest request)
	{
		ConstatFinalUtil.LOGGER.info("---- ordersConfirm ----");
		HttpSession session = request.getSession() ;
		AUsers users = (AUsers) session.getAttribute("users");
		
		/* 查询用户的所有收货地址(启用) */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("usersid",users.getId());
		condMap.put("status", AUsersAddressEnum.STATUS_ENABLE.getStatus());
		List<AUsersAddress> usersAddressList = this.usersService.findCondListUsersAddressService(null, condMap);
		request.setAttribute("usersAddressList", usersAddressList);
		
		/* 获取选中的购物车 */
		String[] shids = request.getParameterValues("shids");
		if(shids != null)
		{
			/**
			 * 查询选中的购物车中的商品
			 */
			List<AUsersCar> usersCarList = new ArrayList<AUsersCar>() ; 
			for (int i = 0; i < shids.length; i++)
			{
				String shid = shids[i];
				/* 根据Id查询数据库里面的购物车 */
				condMap.clear();
				condMap.put("id", shid);
				AUsersCar usersCar = this.usersService.findOneUsersCarService(condMap);
				usersCarList.add(usersCar);
			}
			request.setAttribute("usersCarList", usersCarList);
		}
		
		return "/head/orders/ordersConfirm";
	}
	
	/**
	 * 订单提交
	 * @return
	 */
	@RequestMapping("/ordersInsertSubmit")
	public String ordersInsertSubmit(HttpServletRequest request ,AOrders orders)
	{
		ConstatFinalUtil.LOGGER.info("----ordersInsertSubmit ----");
		HttpSession session = request.getSession() ;
		AUsers users = (AUsers) session.getAttribute("users");
		orders.setUsersId(users.getId());
		/* 设置订单的收货信息(下单那一刻) */
		/* 根据地址Id查询用户地址 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", orders.getAddressId());
		AUsersAddress usersAddress = this.usersService.findOneUsersAddressService(condMap);
		orders.setRegionId(usersAddress.getRegionId());
		orders.setContact(usersAddress.getContact());
		orders.setPhone(usersAddress.getPhone());
		orders.setAddress(usersAddress.getAddress());
		orders.setZipCode(usersAddress.getZipCode());
		
		/* 设置配送方式 */
		orders.setDispatch(Byte.valueOf("0"));
		orders.setDispatchSn("");
		
		/* 设置支付方式 */
		orders.setPaySn("");
		
		/* 设置总重量,总价格, */
		/* 获取选中的购物车 */
		String[] shids = request.getParameterValues("shids");
		if(shids != null)
		{
			List<AOrdersGoods> ordersGoodList = new ArrayList<AOrdersGoods>();
			/**
			 * 查询选中的购物车中的商品
			 * 
			 * 变成订单的购物订单
			 */
			double totalWeight = 0 ;
			double totalPrice = 0 ; 
			for (int i = 0; i < shids.length; i++)
			{
				String shid = shids[i];
				/* 根据Id查询数据库里面的购物车 */
				condMap.clear();
				condMap.put("id", shid);
				AUsersCar usersCar = this.usersService.findOneUsersCarService(condMap);
				AGoods goods = usersCar.getGoods() ; 
				
				AOrdersGoods ordersGoods = new AOrdersGoods();
				ordersGoods.setGoodsId(usersCar.getGoodsId());
				ordersGoods.setName(goods.getName());
				ordersGoods.setPrice(goods.getPrice());
				ordersGoods.setWeight(goods.getWeight());
				ordersGoods.setNum(usersCar.getNum());
				/* 注意丢失精度的问题 */
				ordersGoods.setTotalPrice(ordersGoods.getPrice() * ordersGoods.getNum());
				
				ordersGoods.setCreateTime(new Date());
				ordersGoods.setUpdateTime(new Date());
				ordersGoodList.add(ordersGoods);
				
				totalWeight += ordersGoods.getWeight() * ordersGoods.getNum() ; 
				totalPrice += ordersGoods.getPrice() * ordersGoods.getNum() ; 
			}
			
			orders.setTotalPrice(totalPrice);
			orders.setTotalWeight(totalWeight);
			orders.setOrdersGoodList(ordersGoodList);
		}
		
		orders.setCreateTime(new Date());
		orders.setUpdateTime(new Date());
		orders.setPubTime(new Date());
		JSONObject resultJSON = this.ordersService.saveOneOrdersService(orders);
		this.info = resultJSON.getString("info");
		session.setAttribute("info", info);
		/* 客户端跳转 */
		return "redirect:/head/orders/ordersInfo.htm" ;
	}
	
	/**
	 * 订单查看页面
	 * @return
	 */
	@RequestMapping("/ordersInfo")
	public String ordersInfo()
	{
		return "/head/orders/ordersInfo"; 
	}
}
